<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class model_donation extends CI_Model{
    var $table          = "tbl_donasi";
    var $primary        = "id_donasi";
    var $table_conf     = "tbl_donasi_konfirmasi";
    var $table_member   = "tbl_member";
    
    public function __construct()
    {
        parent::__construct();
    }
    
    function get_all_donation($id){
        $q = $this->db->query("SELECT * FROM " . $this->table . " WHERE id_member = ".$id."  ORDER BY `datemodified` DESC");
        return $q->result();
    }
    
    function get_all_donation_data($id, $limit=0, $offset=0, $conditions=''){
        if( !$id ) return false;
        if( !empty($conditions) ){
            $conditions = str_replace("%jumlah_donasi%",    "jumlah_donasi", $conditions);
            $conditions = str_replace("%datemodified%",     "datemodified", $conditions);
            $conditions = str_replace("%status%",           "status", $conditions);
            $conditions = str_replace("%kategori%",         "kategori", $conditions);
        }
        
        $sql  = 'SELECT SQL_CALC_FOUND_ROWS * FROM ' . $this->table . ' WHERE id_member = ' . $id . '';
        if( !empty($conditions) ) $sql .= $conditions . ' ';
        $sql .= ' ORDER BY datemodified DESC';
        
        if( $limit ) $sql .= ' LIMIT ' . $offset . ', ' . $limit;
        
        $query = $this->db->query($sql);
        if(!$query || !$query->num_rows()) return false;
        
        return $query->result();
    }
    
    function get_donation($id){
        $q = $this->db->query("SELECT * FROM " . $this->table . " WHERE id_donasi = ".$id."");
        return $q->row();
    }
    
    function get_details_donation($id){
        $q = $this->db->query("SELECT * FROM " . $this->table_conf . " WHERE id_donasi = ".$id."");
        return $q->row();
    }
    
    function insert_donation($data){
        if( empty($data) ) return false;
        if( $this->db->insert($this->table, $data) ) {
            $id = $this->db->insert_id();
            return $id;
        };
        return false;
    }
    
    function insert_conf_donation($data){
        if( empty($data) ) return false;
        if( $this->db->insert($this->table_conf, $data) ) {
            $id = $this->db->insert_id();
            return $id;
        };
        return false;
    }
    
    function update_donation($data, $id){
        if( empty($id) ) return false;
        if( empty($data) ) return false;
        if( $this->db->update($this->table, $data, array($this->primary => $id)) ) return true;
        return false;
    }
    function delete($id){
        if( empty($id) ) return false;
        if($this->db->delete($this->table, array($this->primary => $id))) return true;
        else return false; 
    }
    function get_all(){
        $q = $this->db->query("
            SELECT D.id_donasi,M.name,D.datecreated,D.status,D.jumlah_donasi,D.deleted FROM `" . $this->table . "` as D INNER JOIN `".$this->table_member."` as M ON D.id_member = M.id_member");
        return $q->result();
    }
    function get_donation_by_id($id){
        $q = $this->db->query("SELECT D.id_donasi,M.name,D.datecreated,D.status,D.jumlah_donasi,D.deleted FROM `". $this->table ."` as D INNER JOIN `".$this->table_member."` as M ON D.id_member = M.id_member WHERE id_donasi = ".$id."");
        return $q->result();
    }
    function get_donation_confirmation_by_id($id){
        $q = $this->db->query("SELECT * FROM " . $this->table_conf . " WHERE id_donasi = ".$id."");
        return $q->result();
    }
    //cronjob delete donation
    function get_data_by_condition($condition="",$mode="result"){
        $q = $this->db->query("SELECT * FROM `".$this->table."` WHERE ".$condition);
        if(strtolower($mode) == "result") return $q->result();
        else return $q->num_rows();
    }
//    function update_by_condition($condition=""){
//        $query = mysql_real_escape_string("UPDATE ".$this->table." SET deleted=1 WHERE `id_donation` IN ".$condition."");
////        echo $query;exit();
//        if($this->db->query($query)) return TRUE;
//        else return FALSE;
//    }
}
/* End of file model_donation.php */
/* Location: ./application/models/model_donation.php */
